﻿using System;
using System.Collections;
using System.Windows;
using jSLApp.Client.ApplicationBase;
using jSLApp.Client.MainService;
using jSLCore.Client.Presentation.Authorization;
using jSLCore.Client.Presentation.BaseControl.DialogMessage;
using jSLCore.Client.Presentation.BaseControl.Selection;

namespace jSLApp.Client.Forms
{
    public partial class SubscriptionList : SelectControl
    {
        public SubscriptionList()
        {
            InitializeComponent();
        }
        
        private void deleteAll_Clicked(object sender, RoutedEventArgs e)
        {
            var dlg = new QuestionBox("Будут удалены все подписки.Продолжить?", QuestionBoxType.OkCancel);
            dlg.ShowDialog(delegate(object oo, object data)
            {
                DoWork("Удаление подписок", (o, args) => Communication.Proxy.DeleteMySubscriptions(), (sender1, eventArgs) =>
                {
                    if (eventArgs.Error == null)
                    {
                        LoadInitialData();
                    }
                });
            }, this);
        }

        private void delete_Clicked(object sender, RowSelectedEventArgs e)
        {
            var subsription = (Subsription)e.RowObject;

          /*  var dlg = new QuestionBox("Вы больше не будете получать уведомления по указанной теме.Продолжить?",QuestionBoxType.OkCancel);
            dlg.ShowDialog(delegate(object oo, object data)
                               {*/
                                   DoWork("Удаление подписки", (o, args) => Communication.Proxy.DeleteSubscription(args.Int), 
                                       (sender1, eventArgs) =>
                                                                          {
                                                                              if (eventArgs.Error == null)
                                                                              {
                                                                                  LoadInitialData();
                                                                              }
                                                                          }, subsription.id);
                               /*}, this);*/

        }

        public override ISelectableGrid GetSelectableGrid()
        {
            return grid;
        }

        public override bool IsStandartOkCancelEnabledWhenNotSelectable
        {
            get { return true; }
        }

        public override bool IsOkVisibleWhenNotSelectable
        {
            get { return false; }
        }

        public override object CancelContent
        {
            get { return "Закрыть"; }
        }

        public override void LoadInitialData()
        {
            if(jSecurity.IsAuthorized)
            DoWork("Загрузка подписок",(sender, args) =>
                                           {
                                               args.Result = Communication.Proxy.GetMySubscriptions();
                                           },(o, eventArgs) =>
                                                                 {
                                                                     if(eventArgs.Error==null)
                                                                     {
                                                                         grid.ItemsSource = (IEnumerable) eventArgs.Result;
                                                                     }
                                                                 });
        }

        private void goTheme_Clicked(object sender, RowSelectedEventArgs e)
        {
            var rowObject = (Subsription)e.RowObject;

             MainContentContainer.Current.GoCommentable(rowObject.TypeEnum,rowObject.To);
        }
    }
}
